package com.wisdytech.linkdcs.measuretool.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.constants.CommonConstants;
import com.wisdytech.common.constants.DictKeyConstants;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipment.dao.IEqEquipmentDao;
import com.wisdytech.linkdcs.equipment.model.Equipment;
import com.wisdytech.linkdcs.measuretool.dao.MeasureToolDao;
import com.wisdytech.linkdcs.measuretool.model.MeasureTool;
import com.wisdytech.linkdcs.measuretool.service.IMeasureToolService;
import com.wisdytech.linkdcs.system.model.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhanghailin
 * @since 2018-06-15
 */
@Service
@Transactional
public class MeasureToolServiceImpl extends BaseService<MeasureToolDao, MeasureTool> implements IMeasureToolService {

    @Resource
    private MeasureToolDao measureToolDao;

    @Resource
    private IEqEquipmentDao eqEquipmentDao;

    /**
     * 不知道为什么要有这么多查询参数，完全没有什么用处
     *
     * @return pageInfo
     */
    @Override
    public PageInfo<MeasureTool> getMeasureToolByFilter(String measureToolCode,
                                                        String measureToolName,
                                                        String measureToolModel,
                                                        String supplier,
                                                        String areaCode,
                                                        String areaName,
                                                        String department,
                                                        String dutyUser,
                                                        String dutyUserName,
                                                        String productionDateStart,
                                                        String productionDateEnd,
                                                        String checkFlag,
                                                        String checkType,
                                                        Long checkCycle,
                                                        String remark,
                                                        Filter filter) {

        Map<String, Object> params = new HashMap<>();
        params.put("checkTypeParentKey", DictKeyConstants.MEASURE_TOOL_CHECK_TYPE_PARENT_KEY);
        if (StringUtils.isNotBlank(measureToolCode)) {
            params.put("measureToolCode", measureToolCode);
        }
        if (StringUtils.isNotBlank(measureToolModel)) {
            params.put("measureToolModel", measureToolModel);
        }
        if (StringUtils.isNotBlank(measureToolName)) {
            params.put("measureToolName", measureToolName);
        }
        if (StringUtils.isNotBlank(supplier)) {
            params.put("supplier", supplier);
        }
        if (StringUtils.isNotBlank(areaCode)) {
            params.put("areaCode", areaCode);
        }
        if (StringUtils.isNotBlank(areaName)) {
            params.put("areaName", areaName);
        }
        if (StringUtils.isNotBlank(department)) {
            params.put("department", department);
        }
        if (StringUtils.isNotBlank(dutyUser)) {
            params.put("dutyUser", dutyUser);
        }
        if (StringUtils.isNotBlank(dutyUserName)) {
            params.put("dutyUserName", dutyUserName);
        }
        if (StringUtils.isNotBlank(productionDateStart)) {
            params.put("productionDateStart", productionDateStart);
        }
        if (StringUtils.isNotBlank(productionDateEnd)) {
            params.put("productionDateEnd", productionDateEnd);
        }
        if (StringUtils.isNotBlank(checkFlag)) {
            params.put("checkFlag", checkFlag);
        }
        if (StringUtils.isNotBlank(checkType)) {
            params.put("checkType", checkType);
        }
        if (checkCycle != null) {
            params.put("checkCycle", checkCycle);
        }
        if (StringUtils.isNotBlank(remark)) {
            params.put("remark", remark);
        }
        List<MeasureTool> list = this.beforeList(MeasureToolDao.class, filter).getMeasureToolInfoByFilter(params);
        return new PageInfo<>(list);
    }

    @Override
    public Boolean saveOrUpdateMeasureTool(MeasureTool measureTool, SysUser operator) {
        if (operator == null)
            throw new ServiceException("操作人不能为空");
        if (measureTool == null || StringUtils.isBlank(measureTool.getMeasureToolCode()))
            throw new ServiceException("参数错误");
        Integer operateCount;
        //校验编码是否重复
        EntityWrapper<MeasureTool> ew = new EntityWrapper<>();
        ew.eq("measure_tool_code", measureTool.getMeasureToolCode())
                .eq("del_flag", CommonConstants.DEL_FLAG);
        if (StringUtils.isNotBlank(measureTool.getPkId())) {
            ew.ne("pk_id", measureTool.getPkId());
            Integer count = measureToolDao.selectCount(ew);
            if (count > 0) {
                throw new ServiceException("计量器具编码重复");
            }
            measureTool.setUpdateDate(new Date());
            measureTool.setUpdateUser(operator.getUsername());

            MeasureTool mt = measureToolDao.selectById(measureTool.getPkId());
            if ("0".equals(mt.getCheckFlag())&&"1".equals(measureTool.getCheckFlag())) {
                measureTool.setNextCheckDate(measureTool.getCheckDate());
            }

            operateCount = measureToolDao.updateById(measureTool);
        } else {
            Integer count = measureToolDao.selectCount(ew);
            if (count > 0) {
                throw new ServiceException("计量器具编码重复");
            }
            measureTool.setPkId(BaseUtil.getUUID());
            measureTool.setCreateDate(new Date());
            measureTool.setCreateUser(operator.getUsername());
            measureTool.setDelFlag(CommonConstants.DEL_FLAG);

            //插入下次校验开始时间
            measureTool.setNextCheckDate(measureTool.getCheckDate());
            operateCount = measureToolDao.insert(measureTool);
        }

        return operateCount > 0;
    }

    @Override
    public Boolean deleteMeasureTool(String pkIds, SysUser operator) {
        if(operator == null)
            throw new ServiceException("操作人不能为空");
        String[] deletePkIds = pkIds.split(",");
        if(deletePkIds.length == 0) return true;
        List<String> pkIdCollection = Arrays.asList(deletePkIds);
        Integer deleteCount = measureToolDao.deleteBatchIds(pkIdCollection);
        return deleteCount > 0;
    }

    @Override
    public MeasureTool getMeasureToolInfoById(String pkId) {
        if(StringUtils.isBlank(pkId)) {
            return null;
        }
        return this.measureToolDao.getMeasureToolInfoById(pkId);
    }

    @Override
    public MeasureTool getMeasureToolInfoByCode(String measureToolCode) {
        if(StringUtils.isBlank(measureToolCode)) {
            return null;
        }
        return this.measureToolDao.getMeasureToolInfoByCode(measureToolCode);
    }

    @Override
    public PageInfo<MeasureTool> getMeasureToolCheckList(String measureToolCode, String measureToolName, String supplier, String checkType, Long checkCycle, String nextCheckDateStart, String nextCheckDateEnd, String isOverDate,Filter filter) {
        Map<String, Object> params = new HashMap<>();
        params.put("checkTypeParentKey", DictKeyConstants.MEASURE_TOOL_CHECK_TYPE_PARENT_KEY);
        if (StringUtils.isNotBlank(measureToolCode)) {
            params.put("measureToolCode", measureToolCode);
        }
        if (StringUtils.isNotBlank(measureToolName)) {
            params.put("measureToolName", measureToolName);
        }
        if (StringUtils.isNotBlank(supplier)) {
            params.put("supplier", supplier);
        }
        if (StringUtils.isNotBlank(checkType)) {
            params.put("checkType", checkType);
        }
        if (checkCycle != null) {
            params.put("checkCycle", checkCycle);
        }
        if (StringUtils.isNotBlank(isOverDate)) {
            params.put("isOverDate", isOverDate);
        }
        List<MeasureTool> list = this.beforeList(MeasureToolDao.class,filter).getMeasureToolCheckList(params);
        return new PageInfo<>(list);
    }

    @Override
    public MeasureTool getMeasureToolCheckBasicInfo(String pkId) {
        /*EntityWrapper<MeasureTool> ew = new EntityWrapper<>();
        ew.setSqlSelect("pk_id","measure_tool_code","measure_tool_name","department","area_name","measure_range","toleance")
                .eq("pk_id",pkId);*/
        return this.measureToolDao.selectById(pkId);
    }

    @Override
    public PageInfo<MeasureTool> getMeasureToolList(String search, Filter filter) {
        EntityWrapper<MeasureTool> ew = new EntityWrapper<>();
        ew.setSqlSelect("pk_id","measure_tool_code","measure_tool_name")

                .eq("del_flag",CommonConstants.DEL_FLAG);
        if (StringUtils.isNotBlank(search)) {
            ew.like("measure_tool_code",search);
        }
        List<MeasureTool> list = this.beforeList(MeasureToolDao.class,filter).selectList(ew);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Equipment> getEquipmentList(String search, Filter filter) {
        EntityWrapper<Equipment> ew = new EntityWrapper<>();
        ew.setSqlSelect("pk_id","equip_code","equip_name")
                .eq("del_flag",CommonConstants.DEL_FLAG);
        if (StringUtils.isNotBlank(search)) {
            //ew.like("equip_code",search);
            ew.andNew().like("equip_code",search).or().like("equip_name",search);
        }
        List<Equipment> list = this.beforeList(IEqEquipmentDao.class,filter).selectList(ew);
        return new PageInfo<>(list);
    }

    @Override
    public Equipment getEquipmentInfo(String equipCode) {
        Equipment e = new Equipment();
        e.setDelFlag(CommonConstants.DEL_FLAG);
        e.setEquipCode(equipCode);
        return eqEquipmentDao.selectOne(e);
    }
}
